<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
    	if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
	    	redirect('cmladministration','refresh');
	    }
        if($_SESSION['role_id'] != 1)
        {
            redirect('cmladmin/accessdenied','refresh');
        }
    }
    
    public function index()
    {
        $data['title'] = 'User List';
		$data['main'] = 'admin/userlist';
        $data['userlist'] = $this->MUser->getUserList(); 
		$this->load->vars($data);
		$this->load->view('admin/layout');
    }
    
    public function add()
    {
        if($this->input->post('username'))
        {
            $this->MUser->addEditUser();
	  		$this->session->set_flashdata('message','User added');
	  		redirect('userlist','refresh');
        }
        $data['title'] = 'User Add New';
        $data['main'] = 'admin/useradd';    
        $data['roles'] = $this->MUser->getDropdownRoles();
        $data['form'] = $this->MFormGlobal->getButtonsAdd('userlist');   
		$this->load->vars($data);
		$this->load->view('admin/layout');
    }
    
    public function edit($id=0)
    {
        if($this->input->post('username'))
        {
            $this->MUser->addEditUser($this->input->post('user_id'));
	  		$this->session->set_flashdata('message','User edited');
	  		redirect('userlist','refresh');
        }
        $data['title'] = 'User Edit';
        $data['main'] = 'admin/useredit';
        $data['user'] = $this->MUser->getUserById($id);
        $data['roles'] = $this->MUser->getDropdownRoles();
        $data['form'] = $this->MFormGlobal->getButtonsEdit('userlist','user/delete/'.$id);       
		$this->load->vars($data);
		$this->load->view('admin/layout');
    }
    
    public function delete($id)
    {
        $this->MUser->deleteUser($id);
	   $this->session->set_flashdata('message','User deleted');
	   redirect('userlist','refresh');
    }
}